package com.example.administrator.liveapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.Resource;
import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.activity.MallDetailsPage;
import com.example.administrator.liveapp.bean.ShoppingListBean;
import com.example.administrator.liveapp.utils.AppUrl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.CheckedOutputStream;

/**
 * Created by Administrator on 2017/6/21 0021.
 */

public class MallTab_1RecyclerViewAdapter extends RecyclerView.Adapter<MallTab_1RecyclerViewAdapter.ViewHolder> implements View.OnClickListener {
    public List<ShoppingListBean.BookList1Bean> listBean;
    public Context context;

    public MallTab_1RecyclerViewAdapter(List<ShoppingListBean.BookList1Bean> listBean) {
        this.listBean = listBean;
    }

    @Override
    public MallTab_1RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mall_tab_recycle_item, null);
        ViewHolder viewHolder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        context = parent.getContext();
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MallTab_1RecyclerViewAdapter.ViewHolder holder, final int position) {
        String url = listBean.get(position).getBookimgurl();
        String ImageUrl = url.substring(0, url.indexOf("|"));
        Glide.with(context)
                .load(AppUrl.URL + ImageUrl)
                .into(holder.mall_images);
        holder.mall_name.setText(listBean.get(position).getBooktitle());
        holder.mall_money.setText(listBean.get(position).getBookprice() + "元");
        holder.buy_num.setText(listBean.get(position).getSalescount() + "人购买");
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        if (listBean != null && !listBean.equals("")) {
            return listBean.size();
        } else {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView mall_images;
        public TextView mall_name;
        public TextView mall_money;
        public TextView buy_num;

        public ViewHolder(View itemView) {
            super(itemView);
            mall_images = (ImageView) itemView.findViewById(R.id.mall_images);
            mall_name = (TextView) itemView.findViewById(R.id.mall_name);
            mall_money = (TextView) itemView.findViewById(R.id.mall_money);
            buy_num = (TextView) itemView.findViewById(R.id.buy_num);
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    private LiveFragmentAdapter.OnItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(LiveFragmentAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
